package com.example.delll.kugou.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.delll.kugou.R;
import com.example.delll.kugou.ViewHolder.FooterViewHolder;
import com.example.delll.kugou.ViewHolder.RecyclerVeiwHoder;
import com.example.delll.kugou.pojo.ListenUltra;



public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private String TAG = "MainActivity";
    private Context context;
    private ListenUltra listenUltra;
    // 普通布局
    private final int TYPE_ITEM = 1;
    // 脚布局
    private final int TYPE_FOOTER = 2;
    // 当前加载状态，默认为加载完成
    private int loadState = 0;
    // 正在加载
    public final int LOADING = 1;
    // 加载完成
    public final int LOADING_COMPLETE = 2;
    // 加载到底
    public final int LOADING_END = 3;



    public void setListenUltra(ListenUltra listenUltra) {
        this.listenUltra = listenUltra;
        notifyDataSetChanged();
    }


    public RecyclerViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    private long last;

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            long l = System.currentTimeMillis();
            last = l;
            if (l - last < 3000) {
                return TYPE_FOOTER;
            } else{
                return TYPE_FOOTER;
            }
        } else {
            return TYPE_ITEM;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType ==TYPE_ITEM) {
            View rv_view = LayoutInflater.from(context).inflate(R.layout.view_paper_tou_tiao_item_2, parent, false);
            RecyclerVeiwHoder recyclerVeiwHoder = new RecyclerVeiwHoder(rv_view);
            return recyclerVeiwHoder;
        } else if (viewType == TYPE_FOOTER) {
            View footer_view = LayoutInflater.from(context).inflate(R.layout.footer_view, parent, false);
            FooterViewHolder footerViewHolder = new FooterViewHolder(footer_view);
            return footerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecyclerVeiwHoder) {
            RecyclerVeiwHoder recyclerVeiwHoder= (RecyclerVeiwHoder) holder;
            recyclerVeiwHoder.title.setText(listenUltra.getT1348648517839().get(position).getTitle());
            Glide.with(context).load(listenUltra.getT1348648517839().get(position).getImgsrc()).into(recyclerVeiwHoder.icon);
        } else if (holder instanceof FooterViewHolder) {
            Log.d(TAG, "onBindViewHolder: dddddddddddddd");
            FooterViewHolder footerViewHolder= (FooterViewHolder) holder;
            switch (loadState) {
                case LOADING: // 正在加载
                    footerViewHolder.tv_view.setText("正在加载");
                    footerViewHolder.pb_view.setVisibility(View.VISIBLE);
                    footerViewHolder.tv_view.setVisibility(View.VISIBLE);
                    footerViewHolder.ll_layout.setVisibility(View.VISIBLE);
                    break;

                case LOADING_COMPLETE: // 加载完成
                    footerViewHolder.tv_view.setText("加载完成");
                    footerViewHolder.pb_view.setVisibility(View.GONE);
                    footerViewHolder.tv_view.setVisibility(View.VISIBLE);
                    footerViewHolder.ll_layout.setVisibility(View.VISIBLE);
                    break;

                case LOADING_END: // 加载到底
                    footerViewHolder.tv_view.setText("加载到底");
                    footerViewHolder.pb_view.setVisibility(View.GONE);
                    footerViewHolder.tv_view.setVisibility(View.VISIBLE);
                    footerViewHolder.ll_layout.setVisibility(View.VISIBLE);
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return listenUltra==null?0:listenUltra.getT1348648517839().size()+1;
    }

    public void setLoadState(int loadState) {
        this.loadState = loadState;
        notifyDataSetChanged();
    }

}
